#include"main.h"
void buy_book(char* paname)
{
    /***************确认航班****************************************/ 
    int i =0;int j=0;    
    printf("enter number for flight you buy\n");
    char p[10];
    scanf("%s",p);

    int x = ONLY_indext(p);//输入编号对应的航班索引

    if(x == -1){printf("No flight name\n"); return;}

   
/**************************没票情况************************************************/
    else if(FIT[x].Max_passenger<=FIT[x].has_buy)
    {
        printf("No book now,Buy to wait???? 1.Sure  2.Cancle\n");
        scanf("%d",&j);
        if(j == 2){return;}
        if(j == 1)
       Wait_Passenger(x,paname);////////////////////////////////////////////////////      
       return;
    }

/************************************买票******************************************************/
/**************************************私人系统路信息*******/
        printf("Are you sure to buy this flight?\n");
        printf("1 SURE\n2 Cancle\n");
         j=0;
        scanf("%d",&j);
         if(j==1)
         {
          Into_Pas(FIT[x].number,paname,1);
        /***************************管理员系统录入信息*******************************************/
        //1 has_buy    2,passenger.name.flight    
          
                for ( i = 0; i < 10; i++)
                {
                    if(strlen((FIT[x].p[i]).name)==0)//i在空位
                    {
                    break;           
                    }
                }
                (FIT[x].has_buy)++;
   
    
        strcpy((FIT[x].p[i]).name,paname);

        strcpy(FIT[x].p[i].flight_num,FIT[x].number);//x是航班索引

        printf("Buy sucessfull suit is %d\n",i+1);//i是航班内游客索引
        /*********************************************************************************************************************/
       
            if(i == 2){return;}
         }
}
int Wait_Passenger(int x,char* waiter)
{
    
    int i=0;
    for ( i = 0; i < 10; i++)
    {
        if(strlen(FIT[x].list_wait[i]) == 0)//录入候补信息到航班
        {
            FIT[x].has_buy++;
            strcpy(FIT[x].list_wait[i],waiter);
            FIT[x].wait++;
            printf("sucess to buy please wait\n");
            break;
             
        }    
    } 

        for ( i = 0; i < 10; i++)
        {
            if(strlen(pas[i].name)==0)//i在空位
            {
                pas[i].book_type=2;
                strcpy(pas[i].name,waiter);
                strcpy(pas[i].flight_num,FIT[x].number);
                printf("Your ifm sucssfully enter managment\n");//录入个人信息到游客端
                clear();
                getchar();
                return 1;           
            }
        }

       printf("No do\n");
}
                  
int Change_book(char* p)
{
    /*****************************判断用户**********************************************************/
    int x = sreach_indext(p);
    if(pas[x].book_type == 0)
    {
        printf("You don't have any flight\n"); 
        return 0;
    }
    /**********************************判断航班*********************************************************************/
    if(x!=-1)
    {

        //买过票 pas里会有
        char m[10]="0";
        printf("enter flight number\n");
        scanf("%s",m);
        int x2 = ONLY_indext(m);
    if(x2 == -1){printf("no this flight\n");return 0;}
    /***********************************退票和买票***************************************************************************/
   
    
        if(Refund_Ticke(p) == -1)
        {
            printf("refund fail\n");
            return -1;
        }
        else
        buy_book(p);
    
    
    }
    
}
int selfflight(char* p)
{
    int i=0;
    for ( i = 0; i <len2; i++)
    {
        if(strcmp(pas[i].name,p) == 0)
        {
            printf("search sucssful!\n");
            break;
        }
    }
    if(pas[i].book_type == 0)
    {
        printf("You don't have book!\n");
        
    }
    if(pas[i].book_type == 1)
    {
        printf("You  have book!\n");
        printf("Your flight num is %s\n",pas[i].flight_num);
    }
    if(pas[i].book_type == 2)
    {
        printf("You are waiting book!\n");
        printf("Your flight num is %s\n",pas[i].flight_num);
    }
    
   
}
int sreach_indext(char *p)
{
    int i=0;
    for ( i = 0; i <len2; i++)
    {
        if(strcmp(pas[i].name,p) == 0)
        {
            return i;
            break;
        }
    }
   return -1;
}

int  Refund_Ticke(char *p)
{
    int i=0;int j=0;
    int x = sreach_indext(p);  //用户系统寻找
    //int x2 = ONLY_indext(pas[x].flight_num);
    //****************************判断用户购票状态***************************************************************
    if(pas[x].book_type == 0)
    {
        printf("You don't have any flight\n");
        return -1;
    }
    //购票状态退票
    if((pas[x].book_type == 1))
    {    
    printf("You flight is %s\n",pas[x].flight_num);

    printf("1 .refund ticker     2.return");
    scanf("%d",&i);
    if(i == 1)
    { 
        int man_indext =ONLY_indext(pas[x].flight_num);  //航班索引
        //********用户航班和购票清零******       
        memset (pas[x].flight_num,0,10);
        pas[x].book_type=0;
        memset (pas[x].name,0,10);
        /*************管理员信息清除***************/
        int indext = p_index(pas[x],man_indext);
        FIT[man_indext].has_buy--;
        FIT[man_indext].p[indext].book_type=0;
        memset(FIT[man_indext].p[indext].flight_num,0,sizeof(FIT[man_indext].p[indext].flight_num));
        memset(FIT[man_indext].p[indext].name,0,sizeof(FIT[man_indext].p[indext].name));
        
        printf("refund OK\n");
        return 1;
    }
    }
    //**************************原票状态候补***********************************************************************************************
    if((pas[x].book_type == 2))
    {    
    printf("Your wait flight is %s\n",pas[x].flight_num);
    printf("1 .refund ticker     2.return");
    scanf("%d",&i);
    if(i == 1)
    {
        int x3 = waitlist_indext(p,x);
        for ( j = 0; j < len; j++)
        {                  
                memset (pas[x].flight_num,0,10);
                pas[x].book_type=0;
                //FIT[x2].wait--;
              //memset (FIT[x2].list_wait[x3],0,10);
               printf("You sucess to run waiting!\n");
               return 2;
           }
    }
    if(i == 2)return 1;    
    }
    }


int waitlist_indext(char*p,int num)//候补旅客索引
{
    int i =0;
    for ( i = 0; i < 10; i++)
    {
       if(strcmp(p,FIT[num].list_wait[i])==0)
       return i;
    }
    return -1;
    
}

int p_index(passenger pp,int num)//找到已经买票的乘客索引
{
    int i =0;
    for ( i = 0; i < 10; i++)
    {
        if(strcmp(FIT[num].p[i].name,pp.name) ==0)
        {
            return i;
        }
    }
    
}
/**********************录入已经购票信息到pas*********************/
int Into_Pas(char* flight,char*name,int n)
{
    int i =0;
        for ( i = 0;i <len2 ; i++)
        {
            if(strlen(pas[i].name) == 0)
            {
                strcpy(pas[i].name,name);
                break;
            }
            
        }
        pas[i].book_type=n;
        strcpy(pas[i].flight_num,flight);
        return i;
}





















